#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

/* Constant Declarations */
char Look;// Lookahead Character
const int buf_len = 64;

/* Initialize the Variable Area */
int V[26] = {0};

/* Read New Character From Input Stream */
void GetChar() {
  Look = getchar();
}

/* Report an Error */
void WriteLn(const char* s) {
  if (s) {
	printf(s);
  }

  printf("\n");
}

void Error(const char* s) {
  WriteLn(0);

  char buf[buf_len] = {0};
  sprintf(buf, "Error: %s.", s);
  WriteLn(buf);
}

/* Report Error and Halt */
void Abort(const char* s) {
  Error(s);
  exit(0);
}

/* Report What Was Expected */
void Expected(const char* s) {
  char buf[buf_len] = {0};
  sprintf(buf, "%s Expected", s);
  Abort(buf);
}

void NewLine(); // Forward declaration
/* Match a Specific Input Character */
void Match(char x) {
  if (Look == x) {
	GetChar();
	NewLine();
  }
  else {
	char buf[buf_len] = {0};
	sprintf(buf, "'''' %c ''''", x);
	Expected(buf);
  }
}

/* Recognize an Alpha Character  */
int IsAlpha(char c) {
  return isalpha(c);
}

/* Recognize a Decimal Digit */
int IsDigit(char c) {
  return isdigit(c);
}

/* Recognize an Addop */
int IsAddop(char c) {
  if ((c == '+') ||
	  (c == '-')) {
	return 1;
  }
  else {
	return 0;
  }
}

/* Get an Identifier */
char GetName() {
  if (!IsAlpha(Look)) {
	Expected("Name");
  }

  char result = Look;
  GetChar();
  return result;
}

/* Get a Number */
int GetNum() {
  if (!IsDigit(Look)) {
	Expected("Integer");
  }

  int value = 0;
  while (IsDigit(Look)) {
	value = 10 * value + Look - '0';
	GetChar();
  }

  return value;
}

int Expression(); // Forward declaration

/* Parse and Translate a Math Factor */
int Factor() {
  int value;
  if (Look == '(') {
	Match('(');
	value = Expression();
	Match(')');
  }
  else if (IsAlpha(Look)) {
	value = V[GetName()-'a'];
  }
  else {
	value = GetNum();
  }
  
  return value;
}

/* Parse and Translate a Math Term */
int Term() {
  int value = Factor();
  while (Look == '*' || Look == '/') {
	switch (Look) {
	  case '*':
		Match('*');
		value = value * Factor();
		break;
	  case '/':
		Match('/');
		value = value / Factor();
		break;
	}
  }

  return value;
}

/* Parse and Translate an Expression */
int Expression() {
  int value;
  if (IsAddop(Look)) {
	value = 0;
  }
  else {
	value = Term();
  }

  while (IsAddop(Look)) {
	switch(Look) {
	case '+':
	  Match('+');
	  value = value + Term();
	  break;
	case '-':
	  Match('-');
	  value = value - Term();
	  break;
	}
  }
	
  return value;
}

/* Parse and Translate an Assignment Statement */
void Assignment() {
  char Name = GetName();
  Match('=');
  V[Name-'a'] = Expression();
}
  

/* Output a String with Tab */
void EmitLn(const char* s) {
  printf("\t %s\n", s);
}

/* Recognize and Skip Over a Newline */
void NewLine() {
  // On Windows, \r\n in txt.
  // On Unix, \n in txt.
  // On Mac, \r in txt.

  // In Windows Console, is \n(LF)
  if (Look == '\n') {
	GetChar();
  }
}

/* Input Routine */
void Input() {
  Match('!');
  Assignment();
}

/* Output Routine */
void Output() {
  Match('?');
  printf("%d\n", V[GetName()-'a']);
}

/* Initialize */
void Init() {
  GetChar();
}

/* Main Program */
int main(int argc, char** argv) {
  Init();
  do {
	switch (Look) {
	case '?':
	  Output();
	  break;
	case '!':
	  Input();
	  break;
	default:
	  Assignment();
	  break;
	}
	NewLine();
  } while (Look != '.');
}
